package org.jeecg.modules.yw.service;


import java.util.ArrayList;
import java.util.List;

import org.jeecg.modules.yw.mapper.OrderMapper;
import org.jeecg.modules.yw.mapper.OrderQueryMapper;
import org.jeecg.modules.yw.vo.OrderQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.micrometer.core.instrument.util.StringUtils;



@Service
public class OrderQueryService extends ServiceImpl<OrderQueryMapper, OrderQueryVO> {
    @Autowired
    private OrderMapper orderMapper;
    
//	 
    public List<OrderQueryVO> getOders(Page<OrderQueryVO> page, QueryWrapper<OrderQueryVO> queryWrapper, String userId, Integer roleId, Integer isLeader, Integer offset, Integer pageSize, String orderStatus, String orderNo) {
    	List<OrderQueryVO> list = new ArrayList<>();
	   	if (roleId == 1) {
			   queryWrapper.eq("createUserId", userId);
			   queryWrapper.orderByDesc("createTime");

			//list = orderMapper.getOrderList(userId, offset, pageSize);
	   	} else if (roleId == 2) {
	   		if (isLeader != null && isLeader == 1) {
	   			queryWrapper.eq("azbzUserId", userId);
//	   			queryWrapper.or().inSql("id", "select orderId from appointment where bzzId = '"+userId+"'  and delFlag = 0");
	   		    //list = orderMapper.getOrderList1(userId, offset, pageSize);
	   		} else {	   			
//		   		 queryWrapper.and((wrapper) -> {
		   			queryWrapper.inSql("id", "select orderId from appointment where appointId = '"+userId+"' and appointType in (0,1)");
		   			queryWrapper.eq("team_read", 1);
//		         });
	   			
	   		    //list = orderMapper.getOrderList2(userId, offset, pageSize);
	   		}
			queryWrapper.orderByDesc("updateTime");
		} else {
	   		queryWrapper.eq("djshUserId", userId);
			queryWrapper.orderByDesc("updateTime");
			//list = orderMapper.getOrderList3(userId, offset, pageSize);
	   	}



//	   if (StringUtils.isNotBlank(orderStatus)) {
//		   if (orderStatus.contains(",")) {
//			   String str[] = orderStatus.split(",");
//			   String newStr = "";
//			   for (String o : str) {
//				   newStr += o + ",";
//			   }
//			   if (newStr.endsWith(",")) {
//				   newStr = newStr.substring(0, newStr.length() - 1);
//			   }
//			   queryWrapper.in("orderStatus", newStr);
//		   } else {
//			   queryWrapper.eq("orderStatus", orderStatus);
//		   }
//
//	   }
		if (StringUtils.isNotBlank(orderStatus)) {
			if (orderStatus.contains(",")) {
				String[] str = orderStatus.split(",");
				queryWrapper.in("orderStatus", str);
			} else {				
				if (orderStatus.equals("4")) {
					orderStatus = "5";
				}				
				final String status = orderStatus;				
			 	if (roleId == 1 && orderStatus.equals("1")) {
					queryWrapper.and(wrapper ->
							wrapper.nested(w ->
									w.ge("state", 1)
											.eq("orderStatus", "0")
							).or(w ->
									w.eq("orderStatus", "1")
							)
					);
			 	} else {				
			 		queryWrapper.eq("orderStatus", status);
			 	}
			}
		}
	   if (StringUtils.isNotBlank(orderNo)) {
		   queryWrapper.like("orderNo", orderNo);
	   }
       queryWrapper.eq("delFlag", 0);
       
      

//       if (roleId == 2) {
//    	   queryWrapper.orderByAsc("djTime");
//       } else if (roleId == 3){
//    	   queryWrapper.orderByAsc("yyTime");
//       }

       IPage<OrderQueryVO>  ipage = orderMapper.getAllOrders(page, queryWrapper);
       list = ipage.getRecords();    
       return list;
   }  
}



